# ----------------------------------------------------------------------------
# 使用静态库进行编译器测试 避免交叉编译环境下无法通过 CMake 的编译器测试
# ----------------------------------------------------------------------------

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)



# ----------------------------------------------------------------------------
# gnu-arm 工具链相关工具查找
# ----------------------------------------------------------------------------

find_program(CMAKE_C_COMPILER   arm-none-eabi-gcc     REQUIRED)
find_program(CMAKE_CXX_COMPILER arm-none-eabi-g++     REQUIRED)
find_program(CMAKE_ASM_COMPILER arm-none-eabi-gcc     REQUIRED)
find_program(CMAKE_OBJCOPY      arm-none-eabi-objcopy REQUIRED)
find_program(CMAKE_SIZE         arm-none-eabi-size    REQUIRED)



# ----------------------------------------------------------------------------
# gnu-arm 工具链通用选项设置
# ----------------------------------------------------------------------------

# 编译器通用选项
set(COMPILER_FLAGS "")
list(APPEND COMPILER_FLAGS
    "-Wall"
    "-Wextra"
    "-ffunction-sections"
    "-fdata-sections"
    "--specs=nano.specs"
)
string(REPLACE ";" " " COMPILER_FLAGS "${COMPILER_FLAGS}")

# 链接器通用选项
set(LINKER_FLAGS "")
list(APPEND LINKER_FLAGS
    "-Wl,--gc-sections"
    "-Wl,--cref"
    "-Wl,--print-memory-usage"
    "--specs=nosys.specs"
)
string(REPLACE ";" " " LINKER_FLAGS "${LINKER_FLAGS}")

set(CMAKE_C_FLAGS          "${COMPILER_FLAGS}"                           CACHE STRING "C Compiler options"   FORCE)
set(CMAKE_CXX_FLAGS        "${COMPILER_FLAGS} -fno-exceptions -fno-rtti" CACHE STRING "C++ Compiler options" FORCE)
set(CMAKE_ASM_FLAGS        "-x assembler-with-cpp"                       CACHE STRING "ASM Compiler options" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "${LINKER_FLAGS}"                             CACHE STRING "Linker options"       FORCE)



# ----------------------------------------------------------------------------
# gnu-arm 工具链 Debug 和 Release 选项设置
# ----------------------------------------------------------------------------

set(CMAKE_C_FLAGS_DEBUG     "-Og -g3" CACHE STRING "C Compiler options for Debug"   FORCE)
set(CMAKE_CXX_FLAGS_DEBUG   "-Og -g3" CACHE STRING "C++ Compiler options for Debug" FORCE)
set(CMAKE_ASM_FLAGS_DEBUG   "-g"      CACHE STRING "ASM Compiler options for Debug" FORCE)

set(CMAKE_C_FLAGS_RELEASE   "-Os -g0" CACHE STRING "C Compiler options for Debug"   FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "-Os -g0" CACHE STRING "C++ Compiler options for Debug" FORCE)



# ----------------------------------------------------------------------------
# gnu-arm 工具链 root 目录和查找模式设置
# ----------------------------------------------------------------------------

get_filename_component(GNU_ARM_TOOLCHAIN_BIN_DIR  ${CMAKE_C_COMPILER}          DIRECTORY)
get_filename_component(GNU_ARM_TOOLCHAIN_ROOT_DIR ${GNU_ARM_TOOLCHAIN_BIN_DIR} DIRECTORY)

set(CMAKE_FIND_ROOT_PATH "${GNU_ARM_TOOLCHAIN_ROOT_DIR}")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
